import 'package:flutter/cupertino.dart';
import 'package:schoollifedemo/base/routers.dart';
import 'package:schoollifedemo/commonwidget/common_base_viewmodel.dart';

class SettingsPageViewModel extends BaseViewModel {
  final String _accountKey = 'user_account';
  final String _passwordKey = 'user_password';
  final String _autoLoginKey = 'autoLoginKey';

  // 跳转到软件问题提交页面
  Future<void> turnToSoftwareProblemSubmitPage(BuildContext context) async {
    getApplication()
        .getRouter()
        ?.navigateTo(context, RoutersPath.softwareProblemSubmitPage);
  }

  Future<void> logout(BuildContext context) async {
    _clearLocalSaveAccountAndPasswordToSharedPreference(); // 清除本地信息
    _clearSaveIsAutoLoginToSharedPreference(); // 取消自动登录
    // 清除任务栈，并且转到登录页面
    getApplication()
        .getRouter()
        ?.navigateTo(context, RoutersPath.loginPage, clearStack: true);
  }

  // 保存是否自动登录
  Future<void> _clearSaveIsAutoLoginToSharedPreference() async {
    getChannelService()
        .getCommonChannelFunc()
        ?.saveToSharedPreferenceBool(_autoLoginKey, false);
  }

  // 保存账号密码到本地
  Future<void> _clearLocalSaveAccountAndPasswordToSharedPreference() async {
    getChannelService()
        .getCommonChannelFunc()
        ?.saveToSharedPreferenceString(_accountKey, '');
    getChannelService()
        .getCommonChannelFunc()
        ?.saveToSharedPreferenceString(_passwordKey, '');
  }
}
